﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour
{
	#region 单例模型
	private static GameController _instance;
	private void Awake() { _instance = this; }
	public static GameController Instance => _instance;
	#endregion

	public GameObject[] gos;    // 一开始这些为隐藏状态, 开始游戏后才会全部显示

	public Role[] roles;        // 所有的角色Role


	private bool _pause = true;
	public bool IsPause => _pause;      // 是否处于暂停状态

	private int _numCoin;
	public int NumCoin => _numCoin;     // 总金币数量
	public int CntCoin { get; set; }    // 拾取的金币数量

	private int _numEnemy;
	public int NumEnemy => _numEnemy;   // 总怪物数量
	public int CntEnemy { get; set; }   // 消灭的敌人数量

	public static bool _isGod;
	public bool IsGod => _isGod;        // 是否开启上帝模式(子弹无限, 血量锁定)

	public static bool IsNewGame { get; set; }

	private void Start() {
		Debug.Log(IsNewGame);
		if (IsNewGame) {        // 如果是点击了重新开始按钮
			IsNewGame = false;
			UIController.Instance.CloseStartButton();
			Debug.Log("IsGod" + IsGod);
			BeginGame(IsGod);
			// UIController.Instance.tglGod.isOn
		}
	}


	/// <summary>
	/// 开始游戏或重新开始游戏
	/// </summary>
	public void BeginGame(bool isGod) {
		_pause = false;
		StartCoroutine(IBeginGame(isGod));
	}
	private IEnumerator IBeginGame(bool isGod) {
		foreach (GameObject go in gos) {
			if (go.name == "MenuPanel") continue;
			go.SetActive(true);
			if (go.name == "Item") {                // 统计金币总数量
				yield return null;
				for (int i = 0; i < go.transform.childCount; i++) {
					if (go.transform.GetChild(i).GetComponent<Item>().itemType == ItemType.Coin) _numCoin++;
				}
			} else if (go.name == "AllEnemy") {     // 统计Enemy数量
				yield return null;
				for (int i = 0; i < go.transform.childCount; i++) {
					EnemyType type = go.transform.GetChild(i).GetComponent<Enemy>().Type;
					if (type != EnemyType.EnemyJumper && type != EnemyType.EnemyBuzzsaw) _numEnemy++;
				}
				Debug.Log("金币数量:" + NumCoin);
				Debug.Log("怪物数量:" + NumEnemy);
				// _numEnemy = go.transform.childCount;
			}
		}
	}

	/// <summary>
	/// 游戏结束, out玩家获得的评价(star数量)
	/// </summary>
	public void EndGame(Player player) {
		_pause = true;
		Debug.Log("消灭怪物:" + CntEnemy);
		int starNum = 0;
		if (player.CurrentHp > 0)
			starNum++;                  // 到达终点, +1
		if (CntEnemy == NumEnemy)       // 消灭所有Enemy +1
			starNum++;
		if (CntCoin == NumCoin)         // 拾取全部Coin +1
			starNum++;

		UIController.Instance.EndGameUI(starNum);   // 弹出UI
	}


	/// <summary>
	/// 退出游戏
	/// </summary>
	public void ExitGame() {
		Debug.Log("退出游戏");
		Application.Quit();
	}

	/// <summary>
	/// 暂停游戏
	/// </summary>
	public void PauseGame() {
		_pause = true;
		foreach (Role role in roles) {
			if (role != null) {

				role.OnPauseAni();
			}
		}
	}


	/// <summary>
	/// 继续游戏
	/// </summary>
	public void ContinueGame() {
		_pause = false;
		foreach (Role role in roles) {
			if (role != null) {

				role.OnContinueAni();
			}
		}
	}
}
